package man4j;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;

public class CurrentTaskScreen extends AbstractEditTaskScreen {
	public static final String EDIT_CURRENT_TASKS = "Current tasks";
	
	private Command deleteCommand = new Command("Delete", Command.ITEM, 1);
	
	private Command deleteAllCommand = new Command("Delete all", Command.ITEM, 1);
	
	private Command copyCommand = new Command("To common", Command.ITEM, 1);
	
	private Vector taskCommands = new Vector();
	
	private ItemCommandListener taskCommandListener;
	
	public CurrentTaskScreen(final MainMIDlet mainMIDlet) {
		super(mainMIDlet, EDIT_CURRENT_TASKS);
		
		taskCommands.addElement(copyCommand);
		taskCommands.addElement(deleteCommand);
		taskCommands.addElement(deleteAllCommand);
		
		taskCommandListener = new ItemCommandListener() {
			public void commandAction(Command c, Item item) {
				Task t = (Task) item;
				
				if (c == deleteCommand) {
					delete(getItemNum(item));	
					
					mainMIDlet.getGoScreen().deleteTask(t);
				}
				
				if (c == deleteAllCommand) {
					deleteAll();
					
					mainMIDlet.getGoScreen().deleteTasks();
					
					append(new Task());
				}
				
				if (c == copyCommand) {
					Task copyOfTask = new Task(t.getString(), false);
					
					mainMIDlet.getCommonTasksScreen().configureTask(copyOfTask);
					mainMIDlet.getCommonTasksScreen().append(copyOfTask);
				}
			}
		};
	}
	
	public void onSave() {
		getMainMIDlet().getGoScreen().saveTasks();
	}
	
	protected void onTaskAdded(Task task) {
		getMainMIDlet().getGoScreen().addTask(task);		
	}
	
	protected Vector getTaskCommands() {	
		return taskCommands;
	}
	
	protected ItemCommandListener getTaskCommandListener() {	
		return taskCommandListener;
	}
}

